﻿@page "/data"
@page "/data/index"
@attribute [Authorize(Roles = "User, Administrator")]
@inherits OwningComponentBase<ICovenantService>

@using Microsoft.JSInterop
@using Covenant.Core
@using Covenant.Models.Covenant
@using Covenant.Models.Indicators
@using Covenant.Components.Credentials
@using Covenant.Components.Indicators
@using Covenant.Components.Events
@inject IJSRuntime IJSRuntime

<div class="d-flex justify-content-between flex-wrap flex-md-nowrap pb-2 mb-3">
    <h1 class="h2">Data</h1>
</div>

<ul class="nav nav-tabs mb-3" id="data-tab" role="tablist">
    <li class="nav-item">
        <a class="nav-link" id="credentials-tab" data-toggle="tab" href="#credentials" role="tab" aria-controls="credentials" aria-selected="false">
            <span class="fe fe-key"></span>
            Credentials
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link" id="indicators-tab" data-toggle="tab" href="#indicators" role="tab" aria-controls="indicators" aria-selected="false">
            <span class="fe fe-activity"></span>
            Indicators
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link" id="downloads-tab" data-toggle="tab" href="#downloads" role="tab" aria-controls="downloads" aria-selected="false">
            <span class="fe fe-download"></span>
            Downloads
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link" id="screenshots-tab" data-toggle="tab" href="#screenshots" role="tab" aria-controls="screenshots" aria-selected="false">
            <span class="fe fe-camera"></span>
            Screenshots
        </a>
    </li>
</ul>

<div class="tab-content" id="grunt-tab-content">
    <div class="tab-pane fade" id="credentials" role="tabpanel" aria-labelledby="credentials-tab">
        <CredentialTables PasswordCredentials="PasswordCredentials" HashCredentials="HashCredentials" TicketCredentials="TicketCredentials" />
        <a href="/credential/create" class="btn btn-primary">
            <span class="fe fe-plus"></span> Create
        </a>
    </div>
    <div class="tab-pane fade" id="indicators" role="tabpanel" aria-labelledby="indicators-tab">
        <IndicatorTables TargetIndicators="TargetIndicators" NetworkIndicators="NetworkIndicators" FileIndicators="FileIndicators" />
        <a href="/indicator/create" class="btn btn-primary">
            <span class="fe fe-plus"></span> Create
        </a>
    </div>
    <div class="tab-pane fade" id="downloads" role="tabpanel" aria-labelledby="downloads-tab">
        <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center">
            <h4 class="h5">Downloads</h4>
        </div>
        <DownloadEventTable DownloadEvents="DownloadEvents" />
    </div>
    <div class="tab-pane fade" id="screenshots" role="tabpanel" aria-labelledby="screenshots-tab">
        <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center">
            <h4 class="h5">Screenshots</h4>
        </div>
        <ScreenshotEventTable ScreenshotEvents="ScreenshotEvents" />
    </div>
</div>

@code {
    [Parameter]
    public IEnumerable<CapturedPasswordCredential> PasswordCredentials { get; set; }

    [Parameter]
    public IEnumerable<CapturedHashCredential> HashCredentials { get; set; }

    [Parameter]
    public IEnumerable<CapturedTicketCredential> TicketCredentials { get; set; }

    [Parameter]
    public IEnumerable<TargetIndicator> TargetIndicators { get; set; }

    [Parameter]
    public IEnumerable<NetworkIndicator> NetworkIndicators { get; set; }

    [Parameter]
    public IEnumerable<FileIndicator> FileIndicators { get; set; }

    [Parameter]
    public IEnumerable<DownloadEvent> DownloadEvents { get; set; }

    [Parameter]
    public IEnumerable<ScreenshotEvent> ScreenshotEvents { get; set; }

    protected override async Task OnInitializedAsync()
    {
        this.PasswordCredentials = this.PasswordCredentials ?? await Service.GetPasswordCredentials();
        this.HashCredentials = this.HashCredentials ?? await Service.GetHashCredentials();
        this.TicketCredentials = this.TicketCredentials ?? await Service.GetTicketCredentials();
        this.TargetIndicators = this.TargetIndicators ?? await Service.GetTargetIndicators();
        this.NetworkIndicators = this.NetworkIndicators ?? await Service.GetNetworkIndicators();
        this.FileIndicators = this.FileIndicators ?? await Service.GetFileIndicators();
        this.DownloadEvents = this.DownloadEvents ?? await Service.GetDownloadEvents();
        this.ScreenshotEvents = this.ScreenshotEvents ?? await Service.GetScreenshotEvents();
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await IJSRuntime.InvokeAsync<string>("ShowTab", "#credentials-tab");
        }
    }
}